package com.taotao.springboot2.warmflow.GlobalListener;

import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.dromara.warm.flow.core.constant.FlowCons;
import org.dromara.warm.flow.core.entity.Instance;
import org.dromara.warm.flow.core.entity.Task;
import org.dromara.warm.flow.core.listener.GlobalListener;
import org.dromara.warm.flow.core.listener.Listener;
import org.dromara.warm.flow.core.listener.ListenerVariable;
import org.dromara.warm.flow.core.utils.CollUtil;
import org.dromara.warm.flow.core.utils.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 全局监听器: 整个系统只有一个，任务开始、分派、完成和创建时期执行
 *
 * @author warm
 * @since 2024/11/17
 */
@Slf4j
@Component
public class CustomGlobalListener implements GlobalListener {


    @Override
    public void notify(String type, ListenerVariable listenerVariable) {
        log.info("------------------------------type={},listenerVariable={}",type, JSON.toJSONString(listenerVariable));
        switch (type) {
            case Listener.LISTENER_START:
                start(listenerVariable);
                break;
            case Listener.LISTENER_ASSIGNMENT:
                assignment(listenerVariable);
                break;
            case Listener.LISTENER_FINISH:
                finish(listenerVariable);
                break;
            case Listener.LISTENER_CREATE:
                create(listenerVariable);
                break;
        }
    }

    /**
     * 开始监听器，任务开始办理时执行
     * @param variable 监听器变量
     */
    public void start(ListenerVariable variable) {
        log.info("全局开始监听器开始执行......");
        Instance instance = variable.getInstance();
        Map<String, Object> testLeaveMap = variable.getVariable();
        log.info("全局开始监听器执行结束......");

    }

    /**
     * 分派监听器，动态修改代办任务信息
     * @param variable  监听器变量
     */
    public void assignment(ListenerVariable variable) {
        log.info("全局分派监听器开始执行......");
        List<Task> tasks = variable.getNextTasks();
        Instance instance = variable.getInstance();
        if (CollUtil.isNotEmpty(tasks)) {
            for (Task task : tasks) {
                List<String> permissionList = task.getPermissionList();
                // 如果设置了发起人审批，则需要动态替换权限标识
                for (int i = 0; i < permissionList.size(); i++) {
                    String permission = permissionList.get(i);
                    if (StringUtils.isNotEmpty(permission) && permission.contains(FlowCons.WARMFLOWINITIATOR)) {
                        permissionList.set(i, permission.replace(FlowCons.WARMFLOWINITIATOR, instance.getCreateBy()));
                    }
                }
            }
        }
        log.info("全局分派监听器执行结束......");
    }

    /**
     * 完成监听器，当前任务完成后执行
     * @param variable  监听器变量
     */
    public void finish(ListenerVariable variable) {
        log.info("全局完成监听器开始执行......");
        Instance instance = variable.getInstance();
        Map<String, Object> testLeaveMap = variable.getVariable();
        log.info("全局完成监听器执行结束......");
    }

    /**
     * 创建监听器，任务创建时执行
     * @param variable  监听器变量
     */
    public void create(ListenerVariable variable) {
        log.info("全局创建监听器开始执行......");
        Instance instance = variable.getInstance();
        Map<String, Object> testLeaveMap = variable.getVariable();
        log.info("全局创建监听器执行结束......");
    }

}